//
//  XZContactViewController.m
//  XZ_WeChat
//
//  Created by 郭现壮 on 16/9/27.
//  Copyright © 2016年 gxz. All rights reserved.
//

#import "XZContactViewController.h"
#import "NSString+PinYin.h"
#import "IndexView.h"
#import "TableViewHeaderView.h"
#import "TableViewSearchHeaderView.h"
#import "ContactTableViewCell.h"
#import "ContactTableHeaderView.h"
#import "RRContacts.h"

#define NAV_HEIGHT 20
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

static NSString *TableViewHeaderViewIdentifier = @"TableViewHeaderViewIdentifier";
static NSString *TableViewCellIdentifier = @"TableViewCellIdentifier";
static NSString *TableViewSearchHeaderViewIdentifier = @"TableViewSearchHeaderViewIdentifier";

@interface XZContactViewController ()<UITableViewDelegate, UITableViewDataSource, IndexViewDelegate, IndexViewDataSource>
@property (nonatomic, strong) UITableView *demoTableView;
@property (nonatomic, strong) IndexView *indexView;
@property (nonatomic, copy) NSArray *dataSourceArray;                           /**< 数据源数组 */
@property (nonatomic, assign) BOOL isSearchMode;                                /**< 是否有搜索栏  */


/******存放有效索引的数组******/
@property (nonatomic,strong) NSMutableArray *indexs;
/******本地区所有的索引******/
@property (nonatomic,strong) NSArray *allIndexs;
/******数据源方法******/
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation XZContactViewController

#pragma mark - indexs的懒加载
- (NSMutableArray *)indexs{
    if (!_indexs) {
        _indexs = [NSMutableArray array];
    }
    return _indexs;
}
#pragma mark - 数据源的懒加载
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"通讯录";
    // 数组转模型
    NSArray *testArray = @[@"ZeroJ", @"李美建", @"朱耿增", @"田扬", @"曾晶" , @"曾晶" , @"曾晶" , @"曾晶" , @"杨卢青" , @"王森" , @"曾晶",  @"曾好", @"李涵", @"王丹", @"良好", @"124"];
    NSMutableArray *contacts = [[NSMutableArray alloc] init];
    for (NSString *name in testArray) {
        RRContacts *contact = [[RRContacts alloc] init];
        contact.name = name;
        contact.image = [UIImage imageNamed:@"sharemore_friendcard"];
        [contacts addObject:contact];
    }
    
    [self initWithAllData:contacts];
    
    //添加视图
    [self.view addSubview:self.demoTableView];
    [self.view addSubview:self.indexView];
    //默认设置第一组
//    [self.indexView reload];
    [self.indexView setSelectionIndex:0];
}

- (void)initWithAllData:(NSArray *)contacts{
    // 模型name的get方法
    SEL getName = @selector(name);
    // 获取本地区所有的索引
    UILocalizedIndexedCollation *localIndex = [UILocalizedIndexedCollation currentCollation];
    self.allIndexs = localIndex.sectionTitles;
    
    for (int i = 0; i < self.allIndexs.count; i++) {
        [self.dataSource addObject:[NSMutableArray array]];
    }
    // 遍历模型数组
    for (RRContacts *contact in contacts) {
        if (contact == nil) {
            continue;
        }
        // 获取该模型的索引
        NSInteger index = [localIndex sectionForObject:contact collationStringSelector:getName];
        if ([contact.name hasPrefix:@"曾"]) {
            index = [self.allIndexs indexOfObject:@"Z"];
        }
        
        [self.dataSource[index] addObject:contact];
    }
    //    NSLog(@"---%@",self.dataSource);
    // 去除内容为空的索引
    for (int i = 0; i < self.dataSource.count; i++) {
        NSArray *temp = self.dataSource[i];
        if (temp.count != 0) {
            [self.indexs addObject:[NSNumber numberWithInt:i]];
        }
        
    }
}

#pragma mark - TableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.indexs count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger index = [self.indexs[section] integerValue];
    NSArray *temp = self.dataSource[index];
    return temp.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TableViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TableViewHeaderViewIdentifier];
    NSNumber *number = self.indexs[section];
    headerView.letter = self.allIndexs[number.integerValue];
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactTableViewCell *cell = [ContactTableViewCell cellWithTableView:tableView];
    NSNumber *number = self.indexs[indexPath.section];
    NSArray *temp = self.dataSource[number.integerValue];
    RRContacts *contact = temp[indexPath.row];
    cell.contact = contact;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [ContactTableViewCell cellHeight];
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    [self.indexView tableView:tableView willDisplayHeaderView:view forSection:section];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    [self.indexView tableView:tableView didEndDisplayingHeaderView:view forSection:section];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.indexView scrollViewDidScroll:scrollView];
}

#pragma mark - IndexView
- (NSArray<NSString *> *)sectionIndexTitles {
    NSMutableArray *indexArr = [NSMutableArray array];
    for (NSNumber *number in self.indexs) {
        [indexArr addObject:self.allIndexs[number.integerValue]];
    }
    return indexArr;
}

//当前选中组
- (void)selectedSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    
    if (self.isSearchMode && (index == 0)) {
        //搜索视图头视图(这里不能使用scrollToRowAtIndexPath，因为搜索组没有cell)
        [self.demoTableView setContentOffset:CGPointZero animated:NO];
        return;
    }
    [self.demoTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

//将指示器视图添加到当前视图上
- (void)addIndicatorView:(UIView *)view {
    [self.view addSubview:view];
}

#pragma mark - getter
- (UITableView *)demoTableView {
    if (!_demoTableView) {
        _demoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT) style:(UITableViewStylePlain)];
        _demoTableView.delegate = self;
        _demoTableView.dataSource = self;
        _demoTableView.showsVerticalScrollIndicator = NO;
        
        _demoTableView.tableHeaderView = [ContactTableHeaderView headerView];
        
        [_demoTableView registerClass:[TableViewHeaderView class] forHeaderFooterViewReuseIdentifier:TableViewHeaderViewIdentifier];
//        [_demoTableView registerClass:[ContactTableViewCell class] forCellReuseIdentifier:TableViewCellIdentifier];
        [_demoTableView registerClass:[TableViewSearchHeaderView class] forHeaderFooterViewReuseIdentifier:TableViewSearchHeaderViewIdentifier];
        
        UILabel *friendsCountLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        friendsCountLab.textAlignment = NSTextAlignmentCenter;
        friendsCountLab.font = [UIFont systemFontOfSize:14];
        friendsCountLab.text = @"共有好友15名";
        _demoTableView.tableFooterView = friendsCountLab;
    }
    return _demoTableView;
}

- (IndexView *)indexView {
    if (!_indexView) {
        _indexView = [[IndexView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, NAV_HEIGHT, 30, SCREEN_HEIGHT - NAV_HEIGHT)];
        _indexView.delegate = self;
        _indexView.dataSource = self;
    }
    return _indexView;
}

- (NSArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = @[@"e卡地亚", @"法兰克穆勒", @"尊皇", @"蒂芙尼", @"艾米龙", @"NOMOS", @"依波路", @"波尔", @"帝舵", @"名士", @"芝柏", @"e积家", @"尼芙尔", @"泰格豪雅", @"艾美", @"拉芙兰瑞", @"宝格丽", @"古驰", @"香奈儿", @"迪奥", @"雷达", @"豪利时", @"路易.威登", @"蕾蒙威", @"康斯登", @"爱马仕", @"昆仑", @"斯沃琪", @"WEMPE", @"万宝龙", @"浪琴", @"柏莱士", @"雅克德罗", @"雅典", @"帕玛强尼", @"格拉苏蒂原创", @"伯爵", @"百达翡丽", @"爱彼", @"宝玑", @"江诗丹顿", @"宝珀", @"理查德·米勒", @"梵克雅宝", @"罗杰杜彼", @"萧邦", @"百年灵", @"宝齐莱", @"瑞宝", @"沛纳海", @"宇舶", @"真力时", @"万国", @"欧米茄", @"劳力士", @"朗格",@".",@"#"];
    }
    return _dataSourceArray;
}

@end
